from google.appengine.ext import db
from Common.Profile import Profile
from Common.CompanyOrg import Company
from Common.GroupOrg import Group
from Common.DuplicatedInstanceError import DuplicatedInstanceError
from Utility import UserRole

class User(Profile):
    Ref_Company = db.ReferenceProperty(Company, collection_name='Users')
    Ref_Group = db.ReferenceProperty(Group, collection_name="Users")
    
    def put (self):
        # Make sure e-mails are unique for each user
        if (not self.is_saved()) and User.get_by_key_name(self.UserName, self._parent_key):
            raise DuplicatedInstanceError ('User.UserName', self.UserName)
        # make sure the role is user
        self.Role = UserRole.User
        # call the parent method
        db.Model.put (self)
        
    def TransactionPut(self):
        db.run_in_transaction(self.put)